from app import create_app, db

app = create_app()

if __name__ == '__main__':
    with app.app_context():
        # 检查数据库表是否已存在
        inspector = db.inspect(db.engine)
        existing_tables = inspector.get_table_names()
        required_tables = [model.__tablename__ for model in db.Model.__subclasses__()]

        missing_tables = set(required_tables) - set(existing_tables)
        if missing_tables:
            print(f"发现缺失的数据库表: {missing_tables}")
            print("正在创建所有表:", required_tables)
            db.create_all()
            print("数据库表已创建:", inspector.get_table_names())
        else:
            print("所有数据库表已存在,跳过创建")
    app.run(debug=True, port=9001)
